package com.greenant.store.activity;

import it.sephiroth.android.library.widget.HListView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.adapter.GoodsListAdapterForImageItem;
import com.greenant.store.adapter.GoodsListAdapterForTradeStyleLeft;
import com.greenant.store.bean.AntGoods;
import com.greenant.store.bean.AntGoodsStyles;
import com.greenant.store.bean.GoodsKind;
import com.greenant.store.bean.GoodsKindBase;
import com.greenant.store.bean.GoodsList;
import com.greenant.store.bean.StoreInfo;
import com.greenant.store.service.RetrieveDataAsyn;
import com.greenant.store.service.RetrieveDataListener;
import com.greenant.store.utils.UtilTools;
import com.greenant.store.view.ChooseAddGoodsMethodDialog;
import com.greenant.store.view.GoodsHandleDialog;
import com.greenant.store.view.GoodsHandleDialog.ModifyType;
import com.greenant.store.view.GoodsHandleDialog.ModiyResultListener;
import com.lsarah.utils.SystemBarTintManager;

public class MyGoodsActivity extends Activity implements OnClickListener {
	private View mViewBack;
	private View mViewAdd;
	private TextView mTvSwitch;
	private HListView mHLSecondKind;
	private ProgressBar mProgressBar;
	private TextView mTvPrompt;
	private ViewPager mViewPager;
	private View mViewLayoutLeft;
	private View mViewLayoutRight;

	private List<GoodsKind> mGoodsKindList = new ArrayList<GoodsKind>();
	private List<AntGoodsStyles> mGoodsStylesList = new ArrayList<AntGoodsStyles>();
	private List<AntGoods> mDataList = new ArrayList<AntGoods>();
	private List<GoodsKindBase> mGoodsKindBaseList = new ArrayList<GoodsKindBase>();
	
	private LeftLayoutManager mLeftLayoutManager;
	private RightLayoutManager mRightLayoutManager;
	private SecondKindHLAdapter mSecondKindHLAdapter;
	
	private LayoutInflater mLayoutInflater;
	private int mTotalCount = 0;
	private boolean isLoadingData = false;
	private GoodsKind mCurrentGoodsKind;
	private AntGoodsStyles mCurrentGoodsStyles;
	private StoreInfo mCurrentStoreInfo;
	
	private String mStrOffShelf = "";
	private String mStrPromotion = "";
	private String mStrAll = "";
	
	private View lastSelectedView = null;
	private it.sephiroth.android.library.widget.AdapterView.OnItemClickListener mOnItemClickListener = 
			new it.sephiroth.android.library.widget.AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(it.sephiroth.android.library.widget.AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if (isLoadingData) {
				return;
			}
			
			if(lastSelectedView != null) {
				TextView textView = (TextView)lastSelectedView.findViewById(R.id.my_goods_hlistview_item_name);
				textView.setBackgroundResource(R.drawable.my_goods_bg_normal);
				textView.setTextColor(getResources().getColor(android.R.color.black));
			}
			
			lastSelectedView = arg1;
			TextView textView = (TextView)lastSelectedView.findViewById(R.id.my_goods_hlistview_item_name);
			textView.setBackgroundResource(R.drawable.my_goods_bg_selected);
			textView.setTextColor(getResources().getColor(R.color.green));
			
			mCurrentGoodsStyles = mGoodsStylesList.get(arg2);
			mSecondKindHLAdapter.notifyDataSetChanged();
			
			if (!isLoadingData) {
				isLoadingData = true;
				retrieveGoodsList(0, 30);
			}
		}
	};

	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v.equals(mTvSwitch)) {
				if (mViewPager.getCurrentItem() == 0) {
					mViewPager.setCurrentItem(1, false);
				} else {
					mViewPager.setCurrentItem(0, false);
				}
			} 
		}
	};

	private PagerAdapter mPagerAdapter = new PagerAdapter() {

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			if (position == 0) {
				((ViewPager) container).removeView(mViewLayoutLeft);
			} else if (position == 1) {
				((ViewPager) container).removeView(mViewLayoutRight);
			}
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View layoutView = null;
			if (position == 0) {
				((ViewPager) container).addView(mViewLayoutLeft, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				layoutView = mViewLayoutLeft;
			} else if (position == 1) {
				((ViewPager) container).addView(mViewLayoutRight, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				layoutView = mViewLayoutRight;
			}

			return layoutView;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getCount() {
			return 2;
		}
	};
	
	private void retrieveGoodsKindBaseList() {
		Log.d("CEXX", "CEXX => retrieveGoodsKindList");
		// 请求GoodsKindBase list
		RetrieveDataAsyn.getInstance().getGoodsKindBase(-1, new RetrieveDataListener() {
			@Override
			public void dataRetrieved(int requestId, Object data) {
				List<GoodsKindBase> goodsKindList = (List<GoodsKindBase>)data;
				Log.d("CEXX", "CEXX => retrieveGoodsKindList => goodsKindList : " + goodsKindList);
				if(goodsKindList != null) {
					mGoodsKindBaseList.addAll(goodsKindList);
				}
			}
		});
	}
	
	private void refreshData() {
		if(isLoadingData) {
			return;
		}
		isLoadingData = true;
		
		mProgressBar.setVisibility(View.VISIBLE);
		mTvPrompt.setVisibility(View.GONE);
		
		// 读取当前AVUser的StoreInfo
		Log.d("CEXX", "CEXX => loadData => AntHomeApplication.storeInfos => 00 : " + AntHomeApplication.storeInfos);
		if (AntHomeApplication.storeInfos == null || AntHomeApplication.storeInfos.size() == 0) {
			RetrieveDataAsyn.getInstance().getStoreInfoByUserId(-1,
					new RetrieveDataListener() {
						@Override
						public void dataRetrieved(int requestId, Object data) {
							Log.d("CEXX", "CEXX => loadData => AntHomeApplication.storeInfos => 01 : " + AntHomeApplication.storeInfos);
							if (AntHomeApplication.storeInfos == null || AntHomeApplication.storeInfos.size() == 0) {
								// 没有获取到StoreInfo数据
								mProgressBar.setVisibility(View.GONE);
								mTvPrompt.setVisibility(View.VISIBLE);
								mTvPrompt.setText(R.string.goods_list_no_goods);
							} else {
								// 获取到StoreInfo数据
								List<StoreInfo> storeInfoList = (List<StoreInfo>)data;
								if(storeInfoList.size() > 0) {
									mCurrentStoreInfo = storeInfoList.get(0);
								}
								// 请求GoodsKind list
								retrieveGoodsKindList();
								retrieveGoodsCount();
							}
						}
					});
		} else {
			// 获取到StoreInfo数据
			Collection<StoreInfo> storeInfos = AntHomeApplication.storeInfos.values();
			for (StoreInfo storeInfo : storeInfos) {
				mCurrentStoreInfo = storeInfo;
				break;
			}
			// 请求GoodsKind list
			retrieveGoodsKindList();
			retrieveGoodsCount();
		}
	}
	
	private void retrieveGoodsCount() {
		List<String> storeIdList = new ArrayList<String>();
		storeIdList.add(mCurrentStoreInfo.getStoreid());
		RetrieveDataAsyn.getInstance().getGoodsCount(storeIdList, -1, new RetrieveDataListener() {
			@Override
			public void dataRetrieved(int requestId, Object data) {
				if (data != null) {
					GoodsList goodsList = (GoodsList) data;
					AntHomeApplication.sCurrentGoodsCount = goodsList.getTotalNum();
					Log.d("CEXX", "CEXX => AntHomeApplication.sCurrentGoodsCount : " + AntHomeApplication.sCurrentGoodsCount);
				}
			}
		}, null, null);
	}
	
	private void retrieveGoodsKindList() {
		Log.d("CEXX", "CEXX => retrieveGoodsKindList");
		// 请求GoodsKind list
		RetrieveDataAsyn.getInstance().getGoodsKindByUserId(-1, new RetrieveDataListener() {
			@Override
			public void dataRetrieved(int requestId, Object data) {
				List<GoodsKind> goodsKindList = (List<GoodsKind>)data;
				Log.d("CEXX", "CEXX => retrieveGoodsKindList => goodsKindList : " + goodsKindList);
				if(goodsKindList != null) {
					mGoodsKindList.clear();
					mGoodsStylesList.clear();
					
					GoodsKind offShelfGoodsKind = new GoodsKind();
					offShelfGoodsKind.setName(mStrOffShelf);
					mGoodsKindList.add(0, offShelfGoodsKind);
					
					GoodsKind promtionGoodsKind = new GoodsKind();
					promtionGoodsKind.setName(mStrPromotion);
					mGoodsKindList.add(1, promtionGoodsKind);
					for(GoodsKind gk : goodsKindList) {
						if(!TextUtils.isEmpty(gk.getName())) {
							mGoodsKindList.add(gk);
						}
					}
					
					for(GoodsKind gk : mGoodsKindList) {
						List<AntGoodsStyles> gsList = gk.getGoodsStylesList();
						if(gsList == null) {
							gsList = new ArrayList<AntGoodsStyles>();
							gk.setGoodsStylesList(gsList);
						}
						AntGoodsStyles gs = new AntGoodsStyles();
						gs.setName(mStrAll);
						gsList.add(0, gs);
					}
					
					if(mCurrentGoodsKind != null) {
						String kindStr = mCurrentGoodsKind.getName();
						mCurrentGoodsKind = null;
						for(GoodsKind gKind : mGoodsKindList) {
							if(kindStr.equals(gKind.getName())) {
								mCurrentGoodsKind = gKind;
								break;
							}
						}
					}
					
					if(mCurrentGoodsKind == null) {
						mCurrentGoodsKind = offShelfGoodsKind;
					}
					mGoodsStylesList.addAll(mCurrentGoodsKind.getGoodsStylesList());
					mCurrentGoodsStyles = mGoodsStylesList.get(0);
					mLeftLayoutManager.mListViewLeftAdapter.notifyDataSetChanged();
					mSecondKindHLAdapter.notifyDataSetChanged();
					
					mHLSecondKind.post(new Runnable() {
						@Override
						public void run() {
							if(lastSelectedView != null) {
								TextView textView = (TextView)lastSelectedView.findViewById(R.id.my_goods_hlistview_item_name);
								textView.setBackgroundResource(R.drawable.my_goods_bg_normal);
								textView.setTextColor(getResources().getColor(android.R.color.black));
							}
							
							if(mHLSecondKind.getFirstVisiblePosition() == 0) {
								View first = mHLSecondKind.getChildAt(0);
								if(first != null) {
									TextView textView = (TextView)first.findViewById(R.id.my_goods_hlistview_item_name);
									textView.setBackgroundResource(R.drawable.my_goods_bg_selected);
									textView.setTextColor(getResources().getColor(R.color.green));
									lastSelectedView = first;
								}
							}
						}
					});
					
					// 加载商品列表
					retrieveGoodsList(0, 30);
				}
				else {
					mGoodsKindList.clear();
					mGoodsStylesList.clear();
					mDataList.clear();
					mLeftLayoutManager.mListViewLeftAdapter.notifyDataSetChanged();
					mSecondKindHLAdapter.notifyDataSetChanged();
					
					mRightLayoutManager.mAdapter.setDisplayMarketPrice(mStrPromotion.equals(mCurrentGoodsKind.getName()));
					mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.setDisplayMarketPrice(mStrPromotion.equals(mCurrentGoodsKind.getName()));
					mRightLayoutManager.mAdapter.notifyDataSetChanged();
					mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.notifyDataSetChanged();
					
					// 没有获取到StoreInfo数据
					mProgressBar.setVisibility(View.GONE);
					mTvPrompt.setVisibility(View.VISIBLE);
					mTvPrompt.setText(R.string.goods_list_no_goods);
				}
			}
		});
	}
	
	private void retrieveGoodsList(int start, int expectCount) {
		List<String> storeIdList = new ArrayList<String>();
		storeIdList.add(mCurrentStoreInfo.getStoreid());

		int requestId = start;
		RetrieveDataAsyn.getInstance().getGoodsListByKindAndStyle(mCurrentGoodsKind.getName(), mCurrentGoodsStyles.getName(), 
				start, expectCount, storeIdList, 2, requestId, new RetrieveDataListener() {
					String kindRecord = mCurrentGoodsKind.getName();
					String styleRecord = mCurrentGoodsStyles.getName();
					@Override
					public void dataRetrieved(int requestId, Object data) {
						if(!kindRecord.equals(mCurrentGoodsKind.getName()) 
								|| !styleRecord.equals(mCurrentGoodsStyles.getName())) {
							return;
						}
						
						if (data != null) {
							GoodsList goodsList = (GoodsList) data;

							if (requestId == 0) {
								mDataList.clear();
								mTotalCount = goodsList.getTotalNum();
								Log.d("CEXX", "GoodsListTradeStyleActivity => dataRetrieved => mTotalCount : " + mTotalCount);
								mDataList.addAll(goodsList.getGoodList());
								
								mRightLayoutManager.mAdapter.setDisplayMarketPrice(mStrPromotion.equals(mCurrentGoodsKind.getName()));
								mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.setDisplayMarketPrice(mStrPromotion.equals(mCurrentGoodsKind.getName()));
								mRightLayoutManager.mAdapter.notifyDataSetInvalidated();
								mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.notifyDataSetInvalidated();
							} else {
								mDataList.addAll(goodsList.getGoodList());
								mRightLayoutManager.mAdapter.notifyDataSetChanged();
								mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.notifyDataSetChanged();
							}

							isLoadingData = false;
							if (mDataList.size() == 0) {
								mProgressBar.setVisibility(View.GONE);
								mTvPrompt.setVisibility(View.VISIBLE);
								mTvPrompt.setText(R.string.goods_list_no_goods);
							} else {
								mProgressBar.setVisibility(View.GONE);
								mTvPrompt.setVisibility(View.GONE);
							}
						}
					}
				});
		mProgressBar.setVisibility(View.VISIBLE);
		mTvPrompt.setVisibility(View.GONE);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.activity_my_goods, getResources().getColor(R.color.green));
		mViewBack = findViewById(R.id.my_goods_top_arrow_left);
		mViewAdd = findViewById(R.id.my_goods_add);
		mTvSwitch = (TextView) findViewById(R.id.my_goods_switch);
		mHLSecondKind = (HListView) findViewById(R.id.my_goods_second_kind_hl);
		mViewPager = (ViewPager) findViewById(R.id.my_goods_view_pager);
		mProgressBar = (ProgressBar) findViewById(R.id.my_goods_prgressbar);
		mTvPrompt = (TextView) findViewById(R.id.my_goods_tv_prompt);

		mLayoutInflater = LayoutInflater.from(this);
		mViewLayoutLeft = mLayoutInflater.inflate(R.layout.activity_my_goods_left, null);
		mViewLayoutRight = mLayoutInflater.inflate(R.layout.activity_my_goods_right, null);
		mLeftLayoutManager = new LeftLayoutManager();
		mRightLayoutManager = new RightLayoutManager();
		mSecondKindHLAdapter = new SecondKindHLAdapter();
		
		mViewBack.setOnClickListener(this);
		mViewAdd.setOnClickListener(this);
		mHLSecondKind.setSelector(android.R.color.transparent);
		mHLSecondKind.setOnItemClickListener(mOnItemClickListener);
		mHLSecondKind.setAdapter(mSecondKindHLAdapter);
		mTvSwitch.setOnClickListener(mOnClickListener);
		mViewPager.setAdapter(mPagerAdapter);
		
		mStrOffShelf = getResources().getString(R.string.goods_kind_goods_style_off_shelf);
		mStrPromotion = getResources().getString(R.string.goods_kind_promotion);
		mStrAll = getResources().getString(R.string.goods_kind_goods_style_all);
		
		this.retrieveGoodsKindBaseList();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		this.refreshData();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d("CEXX", "MyGoodsActivity => onDestroy");
	}
	
	@Override
	public void onClick(View v) {
		if(v.equals(mViewBack)) {
			this.finish();
		}
		else if(v.equals(mViewAdd)) {
			if(mCurrentStoreInfo == null) {
				return;
			}
			
			Log.d("CEXX", "CEXX => onClick => AntHomeApplication.sCurrentGoodsCount : " + AntHomeApplication.sCurrentGoodsCount);
			if(AntHomeApplication.sCurrentGoodsCount >= 1000) {
				String formatStr = getResources().getString(R.string.out_of_max_goods_count);
				UtilTools.showMyToast(this, String.format(formatStr, 1000));
				return;
			}
			
			new ChooseAddGoodsMethodDialog(this).show();
		}
	}
	
	private class SecondKindHLAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			if (mGoodsStylesList != null) {
				return mGoodsStylesList.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View child = convertView;
			ViewHolder holder = null;
			if (child == null) {
				child = mLayoutInflater.inflate(R.layout.activity_my_goods_hlistview_item, null);
				holder = new ViewHolder(child);
				child.setTag(holder);
			} else {
				holder = (ViewHolder) child.getTag();
			}

//			AntGoodsStyles goodsStyles = mGoodsStylesList.get(position);
//			holder.getTvName().setText(goodsStyles.getName());
			
			AntGoodsStyles goodsStyles = mGoodsStylesList.get(position);
			holder.getTvName().setText(goodsStyles.getName());
			if(mCurrentGoodsStyles != null && goodsStyles.getName().equals(mCurrentGoodsStyles.getName())) {
				holder.getTvName().setBackgroundResource(R.drawable.my_goods_bg_selected);
				holder.getTvName().setTextColor(getResources().getColor(R.color.green));
			}
			else {
				holder.getTvName().setBackgroundResource(R.drawable.my_goods_bg_normal);
				holder.getTvName().setTextColor(getResources().getColor(android.R.color.black));
			}

			return child;
		}

		private class ViewHolder {
			private View mContentView;
			private TextView mTvName;

			public ViewHolder(View contentView) {
				mContentView = contentView;
			}

			public TextView getTvName() {
				if (mTvName == null) {
					mTvName = (TextView) mContentView.findViewById(R.id.my_goods_hlistview_item_name);
				}
				return mTvName;
			}
		}
	}
	
	private class LeftLayoutManager implements OnItemClickListener, OnScrollListener {
		private ListView mListViewLeft;
		private GridView mGridViewLeft;
		private ListViewLeftAdapter mListViewLeftAdapter;
		private GoodsListAdapterForTradeStyleLeft mGoodsListAdapterForTradeStyleLeft;

		public LeftLayoutManager() {
			mListViewLeft = (ListView) mViewLayoutLeft.findViewById(R.id.my_goods_left_listview);
			mListViewLeft.setOnItemClickListener(this);
			mListViewLeftAdapter = new ListViewLeftAdapter();
			mListViewLeft.setAdapter(mListViewLeftAdapter);

			mGridViewLeft = (GridView) mViewLayoutLeft.findViewById(R.id.my_goods_left_gridview);
			mGridViewLeft.setOnItemClickListener(this);
			mGoodsListAdapterForTradeStyleLeft = new GoodsListAdapterForTradeStyleLeft(MyGoodsActivity.this, mDataList);
			mGridViewLeft.setAdapter(mGoodsListAdapterForTradeStyleLeft);
			mGridViewLeft.setOnScrollListener(this);
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if (parent.equals(mListViewLeft)) {
				if (isLoadingData) {
					return;
				}
				
				mCurrentGoodsKind = mGoodsKindList.get(position);
				mListViewLeftAdapter.notifyDataSetChanged();
				
				mGoodsStylesList.clear();
				List<AntGoodsStyles> styleList = mCurrentGoodsKind.getGoodsStylesList();
				for(AntGoodsStyles style : styleList) {
					if(!TextUtils.isEmpty(style.getName())) {
						mGoodsStylesList.add(style);
					}
				}
				mCurrentGoodsStyles = mGoodsStylesList.get(0);
				mSecondKindHLAdapter.notifyDataSetChanged();
				
				mHLSecondKind.post(new Runnable() {
					@Override
					public void run() {
						if(lastSelectedView != null) {
							TextView textView = (TextView)lastSelectedView.findViewById(R.id.my_goods_hlistview_item_name);
							textView.setBackgroundResource(R.drawable.my_goods_bg_normal);
							textView.setTextColor(getResources().getColor(android.R.color.black));
						}
						
						if(mHLSecondKind.getFirstVisiblePosition() == 0) {
							View first = mHLSecondKind.getChildAt(0);
							if(first != null) {
								TextView textView = (TextView)first.findViewById(R.id.my_goods_hlistview_item_name);
								textView.setBackgroundResource(R.drawable.my_goods_bg_selected);
								textView.setTextColor(getResources().getColor(R.color.green));
								lastSelectedView = first;
							}
						}
					}
				});
				
				if (!isLoadingData) {
					isLoadingData = true;
					retrieveGoodsList(0, 30);
					mProgressBar.setVisibility(View.VISIBLE);
					mTvPrompt.setVisibility(View.GONE);
				}
			} else if (parent.equals(mGridViewLeft)) {
				Log.d("CEXX", "CEXX ---> GoodsListActivity ---> onItemClick ---> position : " + position);
				AntGoods goods = mDataList.get(position);
				
				String goodsKindStr = goods.getKind();
				GoodsKind goodsKind = mCurrentGoodsKind;
				for(GoodsKind gk : mGoodsKindList) {
					if(!TextUtils.isEmpty(goodsKindStr) && goodsKindStr.equals(gk.getName())) {
						goodsKind = gk;
						break;
					}
				}
				
				String goodsStyleStr = goods.getStyle();
				AntGoodsStyles goodsStyle = mCurrentGoodsStyles;
				for(AntGoodsStyles antGoodsStyle : mGoodsStylesList) {
					if(!TextUtils.isEmpty(goodsStyleStr) && goodsStyleStr.equals(antGoodsStyle.getName())) {
						goodsStyle = antGoodsStyle;
						break;
					}
				} 
				
				new GoodsHandleDialog(MyGoodsActivity.this, goods, mGoodsKindBaseList, goodsKind, goodsStyle,
						new ModiyResultListener() {
					@Override
					public void modifyResult(boolean successful, ModifyType modifyType, AntGoods antGoods) {
						if(successful) {
							if(ModifyType.ModifyTypeDelete.equals(modifyType)
									|| ModifyType.ModifyTypeOnShelf.equals(modifyType)
									|| ModifyType.ModifyTypeOffShelf.equals(modifyType)) {
								mDataList.remove(antGoods);
								mTotalCount--;
							}
							
							mRightLayoutManager.mAdapter.setDisplayMarketPrice(mStrPromotion.equals(mCurrentGoodsKind.getName()));
							mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.setDisplayMarketPrice(mStrPromotion.equals(mCurrentGoodsKind.getName()));
							mRightLayoutManager.mAdapter.notifyDataSetChanged();
							mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.notifyDataSetChanged();
						}
					}
				}).show();
			}
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			if (totalItemCount >= mTotalCount) {
				return;
			}

			if (isLoadingData) {
				return;
			}

			if (firstVisibleItem + visibleItemCount < totalItemCount) {
				return;
			}

			if (!isLoadingData) {
				isLoadingData = true;
				int start = totalItemCount;
				int expectedCount = 18;
				retrieveGoodsList(start, expectedCount);
			}
		}

		public class ListViewLeftAdapter extends BaseAdapter {

			@Override
			public int getCount() {
				if (mGoodsKindList != null) {
					return mGoodsKindList.size();
				}
				return 0;
			}

			@Override
			public Object getItem(int arg0) {
				return arg0;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View child = convertView;
				ViewHolder holder = null;
				if (child == null) {
					child = mLayoutInflater.inflate(R.layout.activity_my_goods_left_listview_item, null);
					holder = new ViewHolder(child);
					child.setTag(holder);
				} else {
					holder = (ViewHolder) child.getTag();
				}

				GoodsKind goodsKind = mGoodsKindList.get(position);
				holder.getTvName().setText(goodsKind.getName());
				if (goodsKind == mCurrentGoodsKind) {
					holder.getViewLine().setBackgroundResource(R.color.green);
					holder.getTvName().setTextColor(getResources().getColor(R.color.green));
				} else {
					holder.getViewLine().setBackgroundResource(android.R.color.transparent);
					holder.getTvName().setTextColor(getResources().getColor(android.R.color.black));
				}

				return child;
			}

			private class ViewHolder {
				private View mContentView;
				private TextView mTvName;
				private View mViewLine;

				public ViewHolder(View contentView) {
					mContentView = contentView;
				}

				public TextView getTvName() {
					if (mTvName == null) {
						mTvName = (TextView) mContentView.findViewById(R.id.my_goods_left_listview_item_name);
					}
					return mTvName;
				}

				public View getViewLine() {
					if (mViewLine == null) {
						mViewLine = mContentView.findViewById(R.id.my_goods_left_listview_item_line);
					}
					return mViewLine;
				}
			}
		}
	}

	private class RightLayoutManager implements OnItemClickListener, OnScrollListener {
		private GridView mGridViewRight;
		private GoodsListAdapterForImageItem mAdapter;

		public RightLayoutManager() {
			mGridViewRight = (GridView) mViewLayoutRight.findViewById(R.id.my_goods_right_gridview);
			mAdapter = new GoodsListAdapterForImageItem(MyGoodsActivity.this, mDataList);
			mGridViewRight.setAdapter(mAdapter);
			mGridViewRight.setOnScrollListener(this);
			mGridViewRight.setOnItemClickListener(this);
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Log.d("CEXX", "CEXX ---> GoodsListActivity ---> onItemClick ---> position : " + position);
			AntGoods goods = mDataList.get(position);
			
			String goodsKindStr = goods.getKind();
			GoodsKind goodsKind = mCurrentGoodsKind;
			for(GoodsKind gk : mGoodsKindList) {
				if(!TextUtils.isEmpty(goodsKindStr) && goodsKindStr.equals(gk.getName())) {
					goodsKind = gk;
					break;
				}
			}
			
			String goodsStyleStr = goods.getStyle();
			AntGoodsStyles goodsStyle = mCurrentGoodsStyles;
			for(AntGoodsStyles antGoodsStyle : mGoodsStylesList) {
				if(!TextUtils.isEmpty(goodsStyleStr) && goodsStyleStr.equals(antGoodsStyle.getName())) {
					goodsStyle = antGoodsStyle;
					break;
				}
			} 
			
			new GoodsHandleDialog(MyGoodsActivity.this, goods, mGoodsKindBaseList, goodsKind, goodsStyle, 
					new ModiyResultListener() {
				@Override
				public void modifyResult(boolean successful, ModifyType modifyType, AntGoods antGoods) {
					if(successful) {
						if(ModifyType.ModifyTypeDelete.equals(modifyType)
								|| ModifyType.ModifyTypeOnShelf.equals(modifyType)
								|| ModifyType.ModifyTypeOffShelf.equals(modifyType)) {
							mDataList.remove(antGoods);
							mTotalCount--;
						}
						
						mRightLayoutManager.mAdapter.setDisplayMarketPrice(mStrPromotion.equals(mCurrentGoodsKind.getName()));
						mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.setDisplayMarketPrice(mStrPromotion.equals(mCurrentGoodsKind.getName()));
						mRightLayoutManager.mAdapter.notifyDataSetChanged();
						mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.notifyDataSetChanged();
					}
				}
			}).show();
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			if (totalItemCount >= mTotalCount) {
				return;
			}

			if (isLoadingData) {
				return;
			}

			if (firstVisibleItem + visibleItemCount < totalItemCount) {
				return;
			}

			if (!isLoadingData) {
				isLoadingData = true;
				int start = totalItemCount;
				int expectedCount = 18;
				retrieveGoodsList(start, expectedCount);
			}
		}
	}
}
